package soar.template.springboot.service.hello.configuration.mariadb;

import java.util.Arrays;
import java.util.Properties;

import javax.annotation.Resource;
import javax.sql.DataSource;

import org.apache.commons.dbcp2.BasicDataSource;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter;

import soar.template.springboot.service.hello.appconfig.AppConfig;
import soar.template.springboot.service.hello.appconfig.mariadb.Config;

//import com.mchange.v2.c3p0.ComboPooledDataSource;

@Configuration
class MyMariaDBConfiguration
{
    @Resource(name = "appconfig")
    private AppConfig appconfig;

    // @Autowired
    // private MySQLMainConfig mysqlMain;

    // @Bean
    // @Primary
    // public DataSource datasourceMain() throws Exception
    // {
    // ComboPooledDataSource ds = new ComboPooledDataSource();
    // ds.setDriverClass("com.mysql.jdbc.Driver");
    //// String prefix="mysql.db1.";
    //// ds.setJdbcUrl(appProps.getProperty(prefix+"url"));
    //// ds.setUser(appProps.getProperty(prefix+"username"));
    //// ds.setPassword(appProps.getProperty(prefix+"password"));
    // ds.setJdbcUrl(mysqlPropertiesMain.getUrl());
    // ds.setUser(mysqlPropertiesMain.getUsername());
    // ds.setPassword(mysqlPropertiesMain.getPassword());
    // ds.setMaxPoolSize(100);
    // ds.setMaxIdleTime(20);
    // return ds;
    // }

    @Bean(name = "mainDatasource")
    @Primary
    public DataSource mainDatasource() throws Exception
    {
        return create_datasource(appconfig.mariadb.main);
    }

    @Bean(name = "mainEntityManagerFactory")
    @Primary
    public LocalContainerEntityManagerFactoryBean mainEntityManagerFactory()
            throws Exception
    {
        return create_factory("main", mainDatasource());
    }

    // @Autowired
    // private MySQLPropertiesLog mysqlPropertiesLog;

    // @Bean
    // public DataSource datasourceLog() throws Exception
    // {
    // ComboPooledDataSource ds = new ComboPooledDataSource();
    // ds.setDriverClass("com.mysql.jdbc.Driver");
    // ds.setJdbcUrl(mysqlPropertiesLog.getUrl());
    // ds.setUser(mysqlPropertiesLog.getUsername());
    // ds.setPassword(mysqlPropertiesLog.getPassword());
    // ds.setMaxPoolSize(100);
    // ds.setMaxIdleTime(20);
    // return ds;
    // }

    @Bean(name = "logDatasource")
    public DataSource logDatasource() throws Exception
    {
        return create_datasource(appconfig.mariadb.log);
    }

    @Bean(name = "logEntityManagerFactory")
    public LocalContainerEntityManagerFactoryBean logEntityManagerFactory()
            throws Exception
    {
        return create_factory("log", logDatasource());
    }

    private DataSource create_datasource(Config config)
    {
        BasicDataSource ds = new BasicDataSource();
        ds.setDriverClassName("org.mariadb.jdbc.Driver");
        ds.setUrl(config.url);
        ds.setUsername(config.username);
        ds.setPassword(config.password);
        ds.setMaxTotal(100);
        ds.setTestWhileIdle(true);
        ds.setTimeBetweenEvictionRunsMillis(10 * 1000);
        ds.setMinEvictableIdleTimeMillis(20 * 1000);
        ds.setRemoveAbandonedOnBorrow(true);
        ds.setRemoveAbandonedTimeout(180);
        ds.setConnectionInitSqls(Arrays.asList("set names utf8mb4;"));
        return ds;
    }

    private LocalContainerEntityManagerFactoryBean create_factory(
            String persistence_unit_name, DataSource datasource)
    {
        LocalContainerEntityManagerFactoryBean factory = new LocalContainerEntityManagerFactoryBean();

        factory.setPersistenceUnitName(persistence_unit_name);
        factory.setDataSource(datasource);

        HibernateJpaVendorAdapter vendor = new HibernateJpaVendorAdapter();
        vendor.setDatabasePlatform("org.hibernate.dialect.MySQL5Dialect");
        vendor.setGenerateDdl(false);
        vendor.setShowSql(true);
        factory.setJpaVendorAdapter(vendor);

        Properties props = new Properties();
        props.put("hibernate.format_sql", "true");
        props.put("hibernate.hbm2ddl.auto", "validate");
        factory.setJpaProperties(props);

        return factory;
    }
}
